package com.car.dao.impl;

import com.car.dao.AdminLog;
import com.car.domain.Admin;
import com.car.utils.JDBCUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 核验用户名，密码，判断是否存在
 * 传回server
 */
public class AdminLogImpl implements AdminLog {
    JdbcTemplate jt=new JdbcTemplate(JDBCUtil.getDs());
    @Override
    public Admin logCheck(Admin admin) {
        String sql="select * from tab_log where username=? && password=?";
        try {
            admin = jt.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), admin.getUsername(), admin.getPassword());
            return admin;
        } catch (DataAccessException e) {
            return null;
        }
    }
// 如果为null 则数据库中有,名字冲突
    @Override
    public Admin registerCheck(Admin admin ,String string) {
        String sql="select * from tab_log where username=? || sid=?";
        try {
            admin = jt.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), admin.getUsername(),string);
            return null;
        } catch (DataAccessException e) {
            return admin;
        }
    }

    //添加到数据库
    @Override
    public int RegisterAdd(Admin admin,String string) {
        String sql="insert into tab_log values(?,?,?)";
        int update = jt.update(sql,string, admin.getUsername(), admin.getPassword());
        return update;
    }


}
